package net.arvin.mobilebanksystem.fragment;

import java.util.List;

import net.arvin.dao.ResponseHandler;
import net.arvin.mobilebanksystem.R;
import net.arvin.mobilebanksystem.activity.MainActivity;
import net.arvin.model.Card;
import net.arvin.model.User;
import net.arvin.service.CardService;
import net.arvin.service.UserService;
import net.arvin.utils.NetConnectionUtil;
import net.arvin.utils.PreferenceUtils;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;

@SuppressLint("ResourceAsColor")
public class MineFragment extends Fragment implements OnClickListener {
	private RelativeLayout baseInfo;
	private RelativeLayout balanceLayout;
	private RelativeLayout bindCard;
	private RelativeLayout otherthings;

	private TextView cardState;
	
	private TextView nickname;
	private TextView phonenumber;
	private TextView balance;
	private ImageView avatar;

	private List<Card> cards = null;

	private ResponseHandler mResponseHandler;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_mine, null);
		init(view);
		return view;
	}

	private void init(View view) {
		changeTitleState();
		initView(view);
		initListener();
		updateData();
	}

	/*
	 * 该类跳转会打开backBtn和addBtn以及修改title，则在返回该类时让title信息还原
	 */
	private void changeTitleState() {
		MainActivity.mainActivity.hidenBackBtn();
		MainActivity.mainActivity.hidenAddBtn();
		MainActivity.mainActivity.setIsCanOpenMenu(true);
		MainActivity.mainActivity.setTitleTv("我的");
	}

	private void updateData() {
		String data = PreferenceUtils.getPreferCards(getActivity());
		cards = JSON.parseArray(data, Card.class);
		changeCardState();
		data = PreferenceUtils.getPreferUserJson(getActivity());
		User user = JSON.parseObject(data, User.class);
		nickname.setText(user.getNickname());
		phonenumber.setText(user.getPhonenumber());
		balance.setText(user.getAcountbalance() + "");
		if (NetConnectionUtil.isConnectWithTip(getActivity(), "没有联网=_=!")) {
			CardService.tryGetAllCards(
					PreferenceUtils.getPreferPhonenumber(getActivity()),
					getCardsResponseHandler(), getActivity());
		}
		String IMAGE_FILE_PATH = PreferenceUtils.getPreferAvatarPath(getActivity());
		if (IMAGE_FILE_PATH != null) {
			Bitmap photo = BitmapFactory.decodeFile(IMAGE_FILE_PATH);
			avatar.setImageBitmap(photo);
		}
	}

	private ResponseHandler getCardsResponseHandler() {
		mResponseHandler = new ResponseHandler() {

			@Override
			public void onRequsetSucceeded(final Object data) {
				if (data.toString().equals("[]")) {
					Toast.makeText(getActivity(), "没有绑定银行卡~",
							Toast.LENGTH_SHORT).show();
					cards = null;
					PreferenceUtils.modifyStringValueInPreferences(
							PreferenceUtils.Cards, null, getActivity());
				} else {
					cards = JSON.parseArray(data.toString(), Card.class);
					PreferenceUtils.modifyStringValueInPreferences(
							PreferenceUtils.Cards, data.toString(),
							getActivity());
				}
				changeCardState();
				UserService.tryGetUserInfo(
						PreferenceUtils.getPreferPhonenumber(getActivity()),
						getUserResponseHandler(), getActivity());
			}

			@Override
			public void onRequestFailed(final Object data) {
				Toast.makeText(getActivity(), "系统出错!", Toast.LENGTH_SHORT)
						.show();
			}
		};
		return mResponseHandler;
	}

	private ResponseHandler getUserResponseHandler() {
		mResponseHandler = new ResponseHandler() {

			@Override
			public void onRequsetSucceeded(final Object data) {
				User user = JSON.parseObject(data.toString(), User.class);
				PreferenceUtils.modifyStringValueInPreferences(
						PreferenceUtils.User, data.toString(), getActivity());
				balance.setText(user.getAcountbalance() + "");
			}

			@Override
			public void onRequestFailed(final Object data) {
				Toast.makeText(getActivity(), "系统出错!", Toast.LENGTH_SHORT)
						.show();
			}
		};
		return mResponseHandler;
	}

	private void changeCardState() {
		if (cards != null && cards.size() > 0) {
			cardState.setText(cards.size() + "张");
			cardState.setTextColor(R.color.gray_lightColor);
		} else {
			cardState.setText("未绑定");
			cardState.setTextColor(R.color.textDefaultColor);
		}
	}

	private void initListener() {
		baseInfo.setOnClickListener(this);
		balanceLayout.setOnClickListener(this);
		bindCard.setOnClickListener(this);
		otherthings.setOnClickListener(this);
	}

	private void initView(View view) {
		baseInfo = (RelativeLayout) view.findViewById(R.id.base_info);
		balanceLayout = (RelativeLayout) view.findViewById(R.id.balanceLayout);
		bindCard = (RelativeLayout) view.findViewById(R.id.bindCard);
		otherthings = (RelativeLayout) view.findViewById(R.id.else_item1);

		nickname = (TextView) view.findViewById(R.id.nickname);
		phonenumber = (TextView) view.findViewById(R.id.phonenumber);
		avatar = (ImageView) view.findViewById(R.id.avatar);
		balance = (TextView) view.findViewById(R.id.balance);
		cardState = (TextView) view.findViewById(R.id.cardState);
	}

	@Override
	public void onClick(View v) {
		String title = null;
		switch (v.getId()) {
		case R.id.base_info:
			MainActivity.mainActivity.showBackBtn();
			title = "个人设置";
			MainActivity.mainActivity.setTitleTv(title);
			MainActivity.mainActivity.setIsCanOpenMenu(false);
			MainActivity.mainActivity.switchContent(new SettingFragment(),
					false);
			break;
		case R.id.balanceLayout:
			title = "余额详情";
			break;
		case R.id.bindCard:
			MainActivity.mainActivity.showBackBtn();
			title = "我的银行卡";
			MainActivity.mainActivity.setTitleTv(title);
			MainActivity.mainActivity.setIsCanOpenMenu(false);
			MainActivity.mainActivity.showAddBtn();
			MainActivity.mainActivity
					.switchContent(new MyCardFragment(), false);
			break;
		case R.id.else_item1:
			title = "其他";
			break;
		}
	}
}
